package com.A21MobileSales;

import java.util.ArrayList;

import com.A21.Object.SpinnerObject;
import com.A21MobileSales.R;
import com.A21MobileSales.DataBase.DatabaseHelper;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class ProductAdvancedSearch extends Activity {

	DatabaseHelper objDataBase;
	String Brand, Model, Made;
	Spinner spnrBrand, spnrModel, spnrMade;
	Button btnSearch, btnClear;
	private ArrayAdapter<SpinnerObject> BrandAdapter;
	private ArrayAdapter<SpinnerObject> ModelAdapter;
	private ArrayAdapter<SpinnerObject> MadeAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.adv_product_search);
		// getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, 520);
		try {
			objDataBase = new DatabaseHelper(this);
			if (getIntent().hasExtra("Brand")) {
				Brand = getIntent().getStringExtra("Brand");
			}
			if (getIntent().hasExtra("Model")) {
				Model = getIntent().getStringExtra("Model");
			}
			if (getIntent().hasExtra("Made")) {
				Made = getIntent().getStringExtra("Made");
			}

			spnrBrand = (Spinner) findViewById(R.id.spnrBrand);
			spnrModel = (Spinner) findViewById(R.id.spnrModel);
			spnrMade = (Spinner) findViewById(R.id.spnrMake);

			btnSearch = (Button) findViewById(R.id.btnSearch);
			btnClear = (Button) findViewById(R.id.btnClear);
			btnSearch.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					hideSoftKeyboard(ProductAdvancedSearch.this, v);
					Bundle b = new Bundle();
					b.putString("status", "done");
					b.putString("Brand", Brand);
					b.putString("Model", Model);
					b.putString("Made", Made);
					Intent intent = new Intent();
					intent.putExtras(b);
					setResult(RESULT_OK, intent);
					finish();
				}
			});
			btnClear.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					hideSoftKeyboard(ProductAdvancedSearch.this, v);
					Brand="";
					Model="";
					Made="";
					Bundle b = new Bundle();
					b.putString("status", "done");
					b.putString("Brand", Brand);
					b.putString("Model", Model);
					b.putString("Made", Made);
					Intent intent = new Intent();
					intent.putExtras(b);
					setResult(RESULT_OK, intent);
					finish();
				}
			});

			objDataBase = new DatabaseHelper(this);
			SpinnerObject[] arrBrand = objDataBase.GetAllBrand();
			BrandAdapter = new ArrayAdapter<SpinnerObject>(this,
					android.R.layout.simple_spinner_item, arrBrand);
			BrandAdapter
					.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			spnrBrand.setAdapter(BrandAdapter);
			spnrBrand.setOnItemSelectedListener(new OnItemSelectedListener() {
				@Override
				public void onItemSelected(AdapterView<?> Adater, View v,
						int Pos, long ID) {
					SpinnerObject item = (SpinnerObject) spnrBrand
							.getSelectedItem();
					String SelectedItem = item.getCode();
					if(SelectedItem.equals("None")){
						Brand=""; 
					}else{
						Brand=SelectedItem; 
					}
				}

				@Override
				public void onNothingSelected(AdapterView<?> arg0) {
					// TODO Auto-generated method stub
				}
			});

			SpinnerObject[] arrModel = objDataBase.GetAllModel();
			ModelAdapter = new ArrayAdapter<SpinnerObject>(this,
					android.R.layout.simple_spinner_item, arrModel);
			ModelAdapter
					.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			spnrModel.setAdapter(ModelAdapter);

			spnrModel.setOnItemSelectedListener(new OnItemSelectedListener() {
				@Override
				public void onItemSelected(AdapterView<?> Adater, View v,
						int Pos, long ID) {
					SpinnerObject item = (SpinnerObject) spnrModel
							.getSelectedItem();
					String SelectedItem = item.getCode();
					if(SelectedItem.equals("None")){
						Model=""; 
					}else{
						Model=SelectedItem; 
					}
				}

				@Override
				public void onNothingSelected(AdapterView<?> arg0) {
					// TODO Auto-generated method stub
				}
			});

			SpinnerObject[] arrMade = objDataBase.GetAllMade();
			MadeAdapter = new ArrayAdapter<SpinnerObject>(this,
					android.R.layout.simple_spinner_item, arrMade);
			MadeAdapter
					.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			spnrMade.setAdapter(MadeAdapter);

			spnrMade.setOnItemSelectedListener(new OnItemSelectedListener() {
				@Override
				public void onItemSelected(AdapterView<?> Adater, View v,
						int Pos, long ID) {
					SpinnerObject item = (SpinnerObject) spnrMade
							.getSelectedItem();
					String SelectedItem = item.getCode();
					if(SelectedItem.equals("None")){
						Made=""; 
					}else{
						Made=SelectedItem; 
					}
				}

				@Override
				public void onNothingSelected(AdapterView<?> arg0) {
					// TODO Auto-generated method stub
				}
			});

			if (!Brand.equals("")) {
				SelectSpinnerItemByValue(spnrBrand,Brand,BrandAdapter);
			}
			if (!Model.equals("")) {
				SelectSpinnerItemByValue(spnrModel,Model,ModelAdapter);
			}
			if (!Made.equals("")) {
				SelectSpinnerItemByValue(spnrMade,Made,MadeAdapter);
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	
	public  void SelectSpinnerItemByValue(Spinner spnr, String value,ArrayAdapter<SpinnerObject> adapter )
	{ 
	    for (int position = 0; position < adapter.getCount(); position++)
	    {
	    	SpinnerObject item=(SpinnerObject) adapter.getItem(position);  
			String ItemVal=item.getCode();  
	        if(ItemVal.equals( value))
	        {
	            spnr.setSelection(position,false);
	            return;
	        }
	    }
	}
	
	public static void hideSoftKeyboard(Activity activity, View view) {
		InputMethodManager imm = (InputMethodManager) activity
				.getSystemService(activity.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
	}

}
